// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const db = cloud.database()
  const orders = db.collection('orders')
  const openid = wxContext.OPENID

  let query = { _openid: openid }
  if (event.payment_status) query.payment_status = event.payment_status
  if (event.status) query.status = event.status

  const res = await orders.where(query).orderBy('created_at', 'desc').get()
  return { orders: res.data }
} 